/**
 * ReportGear(2011)
 */
package net.abbp.report.designer.components.resume;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import net.abbp.report.context.ReportContext;
import net.abbp.report.designer.menu.MainContextMenu;
import net.abbp.report.model.ReportModel;
import net.abbp.report.model.io.impl.ModelXmlIO;
import net.abbp.report.view.GridView;

import com.reportgear.core.data.SalaryHelper;
import com.reportgear.report.core.api.idname.IdName;
import com.reportgear.report.view.grid.util.GridUtils;

/**
 * 工作区视图
 * 
 * @version 1.0 2011-4-17
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class ResumeTemplateDesignWorkbench extends JPanel implements PropertyChangeListener {
    /**
     * UID
     */
    private static final long serialVersionUID = 2110992827184128902L;
    private GridView gridView;
    private ReportModel reportModel;

    // 当前报表Id
    private int reportId = 0;
    // 当前报表名称
    private String reportName = "";

    public ResumeTemplateDesignWorkbench() {
        this.setLayout(new BorderLayout());
        this.reportModel = new ReportModel();

        this.gridView = this.createJWorksheet();
        this.add(this.gridView, BorderLayout.CENTER);

        ReportContext.INSTANCE.setCurrentView(this.gridView);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("CurrModel")) {
            IdName<Integer> selected = (IdName<Integer>) evt.getNewValue();

            this.reportId = selected.getId();
            this.reportName = selected.getName();

            this.loadReport(this.reportId);
        }
    }

    private GridView createJWorksheet() {
        GridView gridView = new GridView(this.reportModel);
        gridView.setGridActionMenu(new MainContextMenu());

        return gridView;
    }

    /**
     * 加载报表模板
     * 
     * @param id
     *            报表模板
     */
    public void loadReport(int id) {
        if (this.gridView != null) {
            this.remove(this.gridView);
            this.gridView = null;
            this.reportModel = null;
        }

        if (id != 0) {
            String template = SalaryHelper.INSTANCE.loadResumeTemplate(id);
            this.reportModel = (new ModelXmlIO()).read(template);
        }
        this.gridView = this.createJWorksheet();

        this.add(this.gridView);

        this.revalidate();

        GridUtils.doSelectCell(gridView, 0, 0);

        ReportContext.INSTANCE.setCurrentView(this.gridView);
    }

    /**
     * 保存报表
     */
    public void saveReport() {
        ReportModel reportModel = this.reportModel;
        String modelXml = (new ModelXmlIO()).write(reportModel);

        int id = SalaryHelper.INSTANCE.saveResumeTemplate(modelXml, this.reportId, this.reportName, 1);

        if (id == this.reportId) {
            JOptionPane.showMessageDialog(this, "模板已保存!");
        }
    }
}
